import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-flow-control-deploy',
	templateUrl: './flow-control-deploy.component.html',
})
export class FlowControlDeploy implements OnInit {
	validatorOptions = {
		excluded:[":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			getFactoryUrl:{
				validators: {
					notEmpty: {
						message: '请输入配置信息'
					}
				}
			},
			flowName:{
				validators: {
					notEmpty: {
						message: '请输入流向管控名城'
					}
				}
			},
			pushOrderUrl:{
				validators: {
					notEmpty: {
						message: '请输入推送修复后订单的url地址'
					}
				}
			},
		}
	}
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	tableParam:any = {
		searchKey: ''
	}
	formType: any=''
	flowControlDeployFrom:any={
		id: '',
		getFactoryUrl: '',
		pushOrderUrl: '',
		flowName: '',
		createTime: ''
	}
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.load();
	}
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:common.stringTrim(this.tableParam.searchKey)
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("flowConfig/pushOrderConfigList.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	// 添加
	table_add(modal){
		this.formType = 'new';
		this.appletsClose(modal)
		$("#flowControlDeployFrom").data('bootstrapValidator').resetForm();
		modal.show();
	}
	// 关闭弹窗
	appletsClose(modal){
		modal.hide();
		this.flowControlDeployFrom.id = '';
		this.flowControlDeployFrom.pushOrderUrl = '';
		this.flowControlDeployFrom.getFactoryUrl = '';
		this.flowControlDeployFrom.flowName = '';
		this.flowControlDeployFrom.createTime = '';
	}
	// 提交弹窗
	appletsSubmit(modal){
		$('#flowControlDeployFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#flowControlDeployFrom').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			this.jsonApiService.http_obj("flowConfig/editPushOrderConfig.html",this.flowControlDeployFrom).subscribe(data=> {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
					this.appletsClose(modal);
				}
			})
		}
	}
	// 编辑弹窗
	table_edit(row,modal){
		this.formType = 'updata';
		$("#flowControlDeployFrom").data('bootstrapValidator').resetForm();
		this.flowControlDeployFrom.id = row.id;
		this.flowControlDeployFrom.pushOrderUrl = row.pushOrderUrl;
		this.flowControlDeployFrom.getFactoryUrl = row.getFactoryUrl;
		this.flowControlDeployFrom.flowName = row.flowName;
		this.flowControlDeployFrom.createTime = row.createTime;
		modal.show();
	}
	// 删除数据
	table_del(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("flowConfig/delPushOrderConfig.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	obtain(){
		this.jsonApiService.http("flowConfig/getFlowInfoByHand.html", '').subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				this.load();
			}
		})
	}
}